package com.lavegue.webaction;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import com.lavegue.common.Constants;
import com.lavegue.common.ListAction;
import com.lavegue.common.Page;
import com.lavegue.model.Area;
import com.lavegue.model.BisGroup;
import com.lavegue.model.BisType;
import com.lavegue.model.ManageMsg;
import com.lavegue.model.ManageMsgDetail;
import com.lavegue.model.UserInfo;
import com.lavegue.service.AreaService;
import com.lavegue.service.BisGroupService;
import com.lavegue.service.BisTypeService;
import com.lavegue.service.ManageMsgService;
import com.lavegue.service.UserInfoService;
import com.lavegue.util.StringUtil;

/**
 * author: luoweibin
 * date:2012-1-4
 * description:
 *
 */
public class MobanListAction extends ListAction<ManageMsg> {
	private ManageMsgService manageMsgService;
	private Date startTime;
	private Date endTime;
	private String title;
	private int bisType;
	private int bisGroup;
	private List<BisType> allTypes;
	private List<BisGroup> allGroups;
	private BisTypeService bisTypeService;
	private BisGroupService bisGroupService;
	private AreaService areaService;
	private int id;
	private ManageMsg mubanMsg;
	private int msgTypeId;
	private String likeName;
	private String txt;
	private int[] ids;
	private UserInfoService userInfoService;
	private int maybeSendStats = -1;
	//等于1时是发送的预览
	private int fasongYulan;
	public String list() throws Exception {
		
	   if(null==this.getSessionAtrribute(Constants.MAN_SESSION_USER))
	   {
		   return "lg";
	   }
		
			
		allTypes = bisTypeService.findAll();
		allGroups = bisGroupService.findAll();
		//如果是0，表示第一次进入这个页面，如果不是，那应该是发生了错误，再次返回到那个页面
		if (allTypes != null && !allTypes.isEmpty()) {
			if (bisType == 0 ) {
				allGroups =  new ArrayList<BisGroup>();// Arrays.asList(allTypes.get(0).getBisGroups().toArray(new BisGroup[allTypes.get(0).getBisGroups().size()]));
			} else {
				allGroups = Arrays.asList(bisTypeService.findById(bisType).getBisGroups().toArray(new BisGroup[allTypes.get(0).getBisGroups().size()]));
			}
		}
		
		Page<ManageMsg> page = manageMsgService.findPage(maybeSendStats , startTime, endTime, title, bisType, bisGroup, pageNo, pageSize);
		this.setPageObj(page);
		return "msgList";
	}
	   public String findMsg()
		{
		      pageObj=manageMsgService.findManageMsgByContent(pageNo, pageSize,msgTypeId,likeName);
			return "listMsg";
		}
	
	/**
	 * 预览模板
	 * @return
	 * @throws Exception
	 */
	public String yulan() throws Exception {
		String huanhang = "\r\n";
		this.mubanMsg = manageMsgService.findById(id);
		if (this.getSessionUser() != null) {
			StringBuffer sb = new StringBuffer();
			sb.append("姓名：" + this.getSessionUser().getUserName() + huanhang);
			sb.append("所在地区：" + getAreaString(this.getSessionUser()) + huanhang);
			sb.append("所在公司：" + this.getSessionUser().getCompany().getCompanyName() + huanhang);
			sb.append("手机号码：" + this.getSessionUser().getAccount() + huanhang);
			if (!StringUtil.isEmpty(this.getSessionUser().getEmail())) {
				sb.append("电子邮箱：" + this.getSessionUser().getEmail() + huanhang);
			}
			if (!StringUtil.isEmpty(this.getSessionUser().getQq())) {
				sb.append("QQ号码：" + this.getSessionUser().getQq() + huanhang);
			}
			if (!StringUtil.isEmpty(this.getSessionUser().getPersonalHonepage())) {
				sb.append("个人主页：" + this.getSessionUser().getPersonalHonepage() + huanhang);
			}
			if (!StringUtil.isEmpty(this.getSessionUser().getPersonalIntro())) {
				sb.append("个人介绍：" + this.getSessionUser().getPersonalIntro() + huanhang);
			}
			ManageMsgDetail dl = new ManageMsgDetail();
			UserInfo ui = this.userInfoService.findById(this.getSessionUser().getId());
			dl.setPic(ui.getPersonalIco());
			dl.setTxt(sb.toString());
			
			if (fasongYulan == 1) {
				mubanMsg.setTitle(likeName);
				if (mubanMsg.getBisType().getEditAble() == mubanMsg.EDIT_ENABLE) {
					ManageMsgDetail detail = mubanMsg.getManageMsgDetails().get(0);
					detail.setTxt(txt);
				}
			}
			mubanMsg.getManageMsgDetails().add(dl);
		}
		
		return "moban_yulan";
	}
	
	public String addMaybeSendSerach() throws Exception {
		maybeSendStats = 0;
		list();
		return "addMaybeSendSerach";
	}
	
	public String addMaybeSend() {
		if(null==this.getSessionAtrribute(Constants.MAN_SESSION_USER)) {
		   return "lg";
	    }
		for (int msgId : ids) {
			ManageMsg msg = manageMsgService.findById(msgId);
			if (msg != null) {
				msg.setMaybeSend(1);
				msg.setMaybeSendAddTime(new Date());
				manageMsgService.update(msg);
			}
		}
		return maybeSendList();
	}
	
	public String deleteMaybeSend() {
		if(null==this.getSessionAtrribute(Constants.MAN_SESSION_USER)) {
		   return "lg";
	    }
		ManageMsg msg = manageMsgService.findById(id);
		if (msg != null) {
			msg.setMaybeSend(0);
			msg.setMaybeSendAddTime(null);
			manageMsgService.update(msg);
		}
		return maybeSendList();
	}
	
	public String maybeSendList() {
		if(null==this.getSessionAtrribute(Constants.MAN_SESSION_USER)) {
		   return "lg";
	    }
		allTypes = bisTypeService.findAll();
		allGroups = bisGroupService.findAll();
		//如果是0，表示第一次进入这个页面，如果不是，那应该是发生了错误，再次返回到那个页面
		if (allTypes != null && !allTypes.isEmpty()) {
			if (bisType == 0 ) {
				allGroups = new ArrayList<BisGroup>();//Arrays.asList(allTypes.get(0).getBisGroups().toArray(new BisGroup[allTypes.get(0).getBisGroups().size()]));
			} else {
				allGroups = Arrays.asList(bisTypeService.findById(bisType).getBisGroups().toArray(new BisGroup[allTypes.get(0).getBisGroups().size()]));
			}
		}
		Page<ManageMsg> page = manageMsgService.findPage(1, startTime, endTime, title, bisType, bisGroup, pageNo, pageSize);
		this.setPageObj(page);
		return "maybeSendList";
	}
	
	private String getAreaString(UserInfo user) {
		StringBuffer sb = new StringBuffer();
		if(!StringUtil.isEmpty(user.getProvince())) {
			sb.append(areaService.findById(Integer.valueOf(user.getProvince())).getName() + " ");
		}
		if(!StringUtil.isEmpty(user.getCity())) {
			sb.append(areaService.findById(Integer.valueOf(user.getCity())).getName() + " ");
		}
		if(!StringUtil.isEmpty(user.getZone())) {
			sb.append(areaService.findById(Integer.valueOf(user.getZone())).getName() + " ");
		}
		return sb.toString();
	}
	
	
	public UserInfoService getUserInfoService() {
		return userInfoService;
	}
	public void setUserInfoService(UserInfoService userInfoService) {
		this.userInfoService = userInfoService;
	}
	public String delete() throws Exception {
		manageMsgService.deleteById(id);
		this.addActionMessage("删除成功");
		return list();
	}

	
	public int getFasongYulan() {
		return fasongYulan;
	}
	public void setFasongYulan(int fasongYulan) {
		this.fasongYulan = fasongYulan;
	}
	public String showEditPage() throws Exception {
		return "editMsg";
	}
	
	public String saveEdit() throws Exception {
		return list();
	}
	
	public ManageMsgService getManageMsgService() {
		return manageMsgService;
	}

	public void setManageMsgService(ManageMsgService manageMsgService) {
		this.manageMsgService = manageMsgService;
	}

	public Date getStartTime() {
		return startTime;
	}

	public void setStartTime(Date startTime) {
		this.startTime = startTime;
	}

	public Date getEndTime() {
		return endTime;
	}
	
	public ManageMsg getMubanMsg() {
		return mubanMsg;
	}

	public void setMubanMsg(ManageMsg mubanMsg) {
		this.mubanMsg = mubanMsg;
	}
	
	public int getMaybeSendStats() {
		return maybeSendStats;
	}
	public void setMaybeSendStats(int maybeSendStats) {
		this.maybeSendStats = maybeSendStats;
	}
	public void setEndTime(Date endTime) {
		this.endTime = endTime;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public int getBisType() {
		return bisType;
	}

	public void setBisType(int bisType) {
		this.bisType = bisType;
	}

	public int getBisGroup() {
		return bisGroup;
	}

	public void setBisGroup(int bisGroup) {
		this.bisGroup = bisGroup;
	}

	public List<BisType> getAllTypes() {
		return allTypes;
	}

	public void setAllTypes(List<BisType> allTypes) {
		this.allTypes = allTypes;
	}

	public List<BisGroup> getAllGroups() {
		return allGroups;
	}

	public void setAllGroups(List<BisGroup> allGroups) {
		this.allGroups = allGroups;
	}

	public BisTypeService getBisTypeService() {
		return bisTypeService;
	}

	public void setBisTypeService(BisTypeService bisTypeService) {
		this.bisTypeService = bisTypeService;
	}

	public BisGroupService getBisGroupService() {
		return bisGroupService;
	}

	public int getMsgTypeId() {
		return msgTypeId;
	}
	public void setMsgTypeId(int msgTypeId) {
		this.msgTypeId = msgTypeId;
	}
	public String getLikeName() {
		return likeName;
	}
	public void setLikeName(String likeName) {
		this.likeName = likeName;
	}
	public void setBisGroupService(BisGroupService bisGroupService) {
		this.bisGroupService = bisGroupService;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}
	public AreaService getAreaService() {
		return areaService;
	}
	public void setAreaService(AreaService areaService) {
		this.areaService = areaService;
	}
	public String getTxt() {
		return txt;
	}
	public void setTxt(String txt) {
		this.txt = txt;
	}
	public int[] getIds() {
		return ids;
	}
	public void setIds(int[] ids) {
		this.ids = ids;
	}
	
	
	
}

